import domain.Person;
import domain.Student;
import domain.User;
import io.netty.buffer.ByteBufAllocator;
import io.netty.channel.*;
import io.netty.channel.embedded.EmbeddedChannel;
import lombok.extern.slf4j.Slf4j;

import java.nio.charset.StandardCharsets;

/**
 * EmbeddedChannel 快捷测试
 *
 * @author ganbo
 * @date 2022/12/7 21:23
 */

@Slf4j
public class EmbeddedChannelTest2 {

    public static void main(String[] args) {

        ChannelInboundHandlerAdapter h1 = new ChannelInboundHandlerAdapter() {
            @Override
            public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
                log.info("h1");
                super.channelRead(ctx, msg);
            }
        };

        ChannelInboundHandlerAdapter h2 = new ChannelInboundHandlerAdapter() {
            @Override
            public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
                log.info("h2");
                super.channelRead(ctx, msg);
            }
        };

        ChannelInboundHandlerAdapter h3 = new ChannelInboundHandlerAdapter() {
            @Override
            public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
                log.info("h3");
//                ctx.writeAndFlush("hello world".getBytes(StandardCharsets.UTF_8));
                ctx.channel().writeAndFlush("hello world");
                super.channelRead(ctx, msg);

            }
        };

        ChannelInboundHandlerAdapter h4 = new ChannelInboundHandlerAdapter() {
            @Override
            public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
                log.info("h4");
//                ctx.writeAndFlush("hello world".getBytes(StandardCharsets.UTF_8));
//                ctx.channel().writeAndFlush("hello world");
                super.channelRead(ctx, msg);
//                ctx.channel().writeAndFlush("hello world");
            }
        };
        ChannelOutboundHandlerAdapter o1 = new ChannelOutboundHandlerAdapter() {
            @Override
            public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
                log.info("o1");
                super.write(ctx, msg, promise);
            }
        };
        ChannelOutboundHandlerAdapter o2 = new ChannelOutboundHandlerAdapter() {
            @Override
            public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
                log.info("o2");
                super.write(ctx, msg, promise);
            }
        };


        //添加handler并且活的channel
        EmbeddedChannel channel = new EmbeddedChannel(h1, o2, h2, h3, h4, o1);

//        //模拟入栈操作
        channel.writeInbound(ByteBufAllocator.DEFAULT.buffer().writeBytes("Hello".getBytes(StandardCharsets.UTF_8)));
    }
}
